package com.readfeed.app.dialogs;

import android.app.Dialog;
import android.app.DialogFragment;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.os.Bundle;
import android.view.KeyEvent;

public class ProgressDialogFragment extends DialogFragment {
	
	public static ProgressDialogFragment newInstance(int messageId) {
		ProgressDialogFragment dialogFragment = new ProgressDialogFragment ();	
		dialogFragment.setRetainInstance(true);
		Bundle args = new Bundle();
		args.putInt("message", messageId);
		dialogFragment.setArguments(args);
		
		return dialogFragment;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		setRetainInstance(true);
		setCancelable(false);
	}
 
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		int message = getArguments().getInt("message");
		final ProgressDialog dialog = new ProgressDialog(getActivity());
		dialog.setMessage(getString(message));
		dialog.setIndeterminate(true);
		dialog.setCancelable(false);
 
		// Disable the back button
		OnKeyListener keyListener = new OnKeyListener() {
 
			@Override
			public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				
				if( keyCode == KeyEvent.KEYCODE_BACK){					
					return true;
				}
				return false;
			} 	
		};
		
		dialog.setOnKeyListener(keyListener);
		return dialog;
	}
	
	@Override
	 public void onDestroyView() {
	     if (getDialog() != null && getRetainInstance())
	         getDialog().setDismissMessage(null);
	         super.onDestroyView();
	 }	
}
